#include <ctype.h>
#include <iso646.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int compare_longs(const void* a, const void* b);

int main()
{
	long n; scanf("%ld", &n);
	long *arr = (long*)malloc(n * sizeof(long)), sum = 0;
	for (long i = 0; i < n; i++) {
		scanf("%ld", arr + i);
		sum += arr[i];
	}

	long avg = sum / n;
	long* c = (long*)malloc(n * sizeof(long)); c[0] = 0;
	for (long i = 1; i < n; i++) {
		c[i] = c[i - 1] + arr[i - 1] - avg;
	}

	qsort(c, n, sizeof n, compare_longs);
	long res = 0;
	for (int i = 0, mid = c[n + 1 >> 1]; i < n; i++)
		res += labs(c[i] - mid);

	printf("%ld",res);
	return 0;
}

int compare_longs(const void* a, const void* b)
{
	long arg1 = *(const long*)a, arg2 = *(const long*)b;
	return (arg1 > arg2) - (arg1 < arg2); // 可行的简写
}
